//
//  H5WebView.m
//  JieJieKan
//
//  Created by 极速现金侠 on 2017/6/19.
//  Copyright © 2017年 innext. All rights reserved.
//

#import "H5WebView.h"

@interface H5WebView ()<WKNavigationDelegate,WKUIDelegate>

@end

@implementation H5WebView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = BACKGROUND_COLOR;
        self.allowsBackForwardNavigationGestures = YES;
        self.userInteractionEnabled = YES;
        self.UIDelegate = self;
        self.navigationDelegate = self;
        
        [self progressView];
        

    }
    return self;
}


- (UIProgressView *)progressView{
    if (_progressView == nil) {
       
        _progressView =  [[UIProgressView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 1)];
        _progressView.tintColor = BLUE_COLOR;
        [self addSubview:_progressView];
    }
    
    return _progressView;
}






//#pragma mark WKUIDelegate
//
////alert 警告框
//-(void)webView:(WKWebView *)webView runJavaScriptAlertPanelWithMessage:(NSString *)message initiatedByFrame:(WKFrameInfo *)frame completionHandler:(void (^)(void))completionHandler{
//    
//    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"警告" message:@"调用alert提示框" preferredStyle:UIAlertControllerStyleAlert];
//    [alert addAction:[UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
//        completionHandler();
//    }]];
//    
//    
//    if ([self.webViewDelegate respondsToSelector:@selector(persentViewController:)]) {
//        [self.webViewDelegate persentViewController:alert];
//    }
//    
////    [self presentViewController:alert animated:YES completion:nil];
//    NSLog(@"alert message:%@",message);
//    
//}
//
////confirm 确认框
//-(void)webView:(WKWebView *)webView runJavaScriptConfirmPanelWithMessage:(NSString *)message initiatedByFrame:(WKFrameInfo *)frame completionHandler:(void (^)(BOOL))completionHandler{
//    
//    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"确认框" message:@"调用confirm提示框" preferredStyle:UIAlertControllerStyleAlert];
//    [alert addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
//        completionHandler(YES);
//    }]];
//    [alert addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
//        completionHandler(NO);
//    }]];
//    
//    if ([self.webViewDelegate respondsToSelector:@selector(persentViewController:)]) {
//        [self.webViewDelegate persentViewController:alert];
//    }
////    [self presentViewController:alert animated:YES completion:NULL];
//    
//    NSLog(@"confirm message:%@", message);
//    
//}
//
//- (void)webView:(WKWebView *)webView runJavaScriptTextInputPanelWithPrompt:(NSString *)prompt defaultText:(nullable NSString *)defaultText initiatedByFrame:(WKFrameInfo *)frame completionHandler:(void (^)(NSString * __nullable result))completionHandler {
//    
//    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"输入框" message:@"调用输入框" preferredStyle:UIAlertControllerStyleAlert];
//    [alert addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
//        textField.textColor = [UIColor blackColor];
//    }];
//    
//    [alert addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
//        completionHandler([[alert.textFields lastObject] text]);
//    }]];
//    
////    [self presentViewController:alert animated:YES completion:NULL];
//    if ([self.webViewDelegate respondsToSelector:@selector(persentViewController:)]) {
//        [self.webViewDelegate persentViewController:alert];
//    }
//}
//





// 2 页面开始加载
- (void)webView:(WKWebView *)webView didStartProvisionalNavigation:(WKNavigation *)navigation{
//    NSString *ulr = webView.URL.absoluteString;
    NSLog(@"页面开始加载");
}

// 4 开始获取到网页内容时返回
- (void)webView:(WKWebView *)webView didCommitNavigation:(WKNavigation *)navigation{
    
    NSLog(@"开始获取到网页内容");
}

// 5 页面加载完成之后调用
- (void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation{
//    NSString *ulr = webView.URL.absoluteString;
    NSLog(@"页面加载完成");
}

// 页面加载失败时调用
- (void)webView:(WKWebView *)webView didFailProvisionalNavigation:(WKNavigation *)navigation{
//    NSString *ulr = webView.URL.absoluteString;
    NSLog(@"加载失败");
}





@end
